$OpenBSD: patch-qmake_generators_unix_unixmake2_cpp,v 1.1.1.1 2016/12/25 14:13:11 zhuk Exp $
1. Un-hardcode system paths and compiler flags.
2. Make sure some paths are listed before others, needed to
   make sure egcc won't pick up /usr/include before its own
   directories.
3. Respect OpenBSD shared library naming rules.
--- qmake/generators/unix/unixmake2.cpp.orig	Fri Sep 16 08:49:42 2016
+++ qmake/generators/unix/unixmake2.cpp	Sat Dec 17 14:16:12 2016
@@ -69,6 +69,10 @@ UnixMakefileGenerator::writeMakefile(QTextStream &t)
     if (writeDummyMakefile(t))
         return true;
 
+    t << "LOCALBASE?=" << LOCALBASE << endl
+      << "X11BASE?="   << X11BASE   << endl
+      << endl;
+
     if (project->first("TEMPLATE") == "app" ||
         project->first("TEMPLATE") == "lib" ||
         project->first("TEMPLATE") == "aux") {
@@ -171,17 +175,17 @@ UnixMakefileGenerator::writeMakeParts(QTextStream &t)
     ProStringList &bundledFiles = project->values("QMAKE_BUNDLED_FILES");
 
     t << "####### Compiler, tools and options\n\n";
-    t << "CC            = " << var("QMAKE_CC") << endl;
-    t << "CXX           = " << var("QMAKE_CXX") << endl;
-    t << "DEFINES       = "
+    t << "CC           ?= " << var("QMAKE_CC") << endl;
+    t << "CXX          ?= " << var("QMAKE_CXX") << endl;
+    t << "DEFINES      += "
       << varGlue("PRL_EXPORT_DEFINES","-D"," -D"," ")
       << varGlue("DEFINES","-D"," -D","") << endl;
-    t << "CFLAGS        = " << var("QMAKE_CFLAGS") << " $(DEFINES)\n";
-    t << "CXXFLAGS      = " << var("QMAKE_CXXFLAGS") << " $(DEFINES)\n";
-    t << "INCPATH       =";
+    t << "CFLAGS        = " << var("QMAKE_CFLAGS") << " $(DEFINES) $(COPTS)\n";
+    t << "CXXFLAGS      = " << var("QMAKE_CXXFLAGS") << " $(DEFINES) $(CXXOPTS)\n";
+    t << "INCPATH      +=";
     {
         QString isystem = var("QMAKE_CFLAGS_ISYSTEM");
-        const ProStringList &incs = project->values("INCLUDEPATH");
+        ProStringList incs = project->values("QMAKE_INCDIR_PREPEND") + project->values("INCLUDEPATH");
         for(int i = 0; i < incs.size(); ++i) {
             const ProString &inc = incs.at(i);
             if (inc.isEmpty())
@@ -201,16 +205,17 @@ UnixMakefileGenerator::writeMakeParts(QTextStream &t)
     writeDefaultVariables(t);
 
     if(!project->isActiveConfig("staticlib")) {
-        t << "LINK          = " << var("QMAKE_LINK") << endl;
-        t << "LFLAGS        = " << var("QMAKE_LFLAGS") << endl;
-        t << "LIBS          = $(SUBLIBS) " << fixLibFlags("QMAKE_LIBS").join(' ') << ' '
+        t << "LINK         ?= " << var("QMAKE_LINK") << endl;
+        t << "LFLAGS        = " << var("QMAKE_LFLAGS") << " $(LDFLAGS)" << endl;
+        t << "LIBS         += $(SUBLIBS) " << fixLibFlags("QMAKE_LIBS").join(' ') << ' '
                                            << fixLibFlags("QMAKE_LIBS_PRIVATE").join(' ') << endl;
     }
 
+    // XXX QMAKE_AR includes args like "cqs", while AR envvar doesn't
     t << "AR            = " << var("QMAKE_AR") << endl;
     t << "RANLIB        = " << var("QMAKE_RANLIB") << endl;
-    t << "SED           = " << var("QMAKE_STREAM_EDITOR") << endl;
-    t << "STRIP         = " << var("QMAKE_STRIP") << endl;
+    t << "SED          ?= " << var("QMAKE_STREAM_EDITOR") << endl;
+    t << "STRIP        ?= " << var("QMAKE_STRIP") << endl;
 
     t << endl;
 
@@ -279,12 +284,19 @@ UnixMakefileGenerator::writeMakeParts(QTextStream &t)
             t << "TARGET0       = " << fileVar("TARGET_") << endl;
         } else if (!project->isActiveConfig("unversioned_libname")) {
             t << "TARGET0       = " << fileVar("TARGET_") << endl;
-            if (project->isEmpty("QMAKE_HPUX_SHLIB")) {
-                t << "TARGETD       = " << fileVar("TARGET_x.y.z") << endl;
-                t << "TARGET1       = " << fileVar("TARGET_x") << endl;
-                t << "TARGET2       = " << fileVar("TARGET_x.y") << endl;
+            if (!project->isEmpty("QMAKE_OPENBSD_SHLIB")) {
+                t << "LIB" << fileVar("QMAKE_ORIG_TARGET") << "_VERSION   ?= "
+                  << project->first("VER_MAJ") << "." << project->first("VER_MIN")
+                  << endl;
+                t << "TARGETD       = " << fileVar("TARGET_x.y") << endl;
             } else {
-                t << "TARGETD       = " << fileVar("TARGET_x") << endl;
+                if (project->isEmpty("QMAKE_HPUX_SHLIB")) {
+                    t << "TARGETD       = " << fileVar("TARGET_x.y.z") << endl;
+                    t << "TARGET1       = " << fileVar("TARGET_x") << endl;
+                    t << "TARGET2       = " << fileVar("TARGET_x.y") << endl;
+                } else {
+                    t << "TARGETD       = " << fileVar("TARGET_x") << endl;
+                }
             }
         }
     }
@@ -605,7 +617,8 @@ UnixMakefileGenerator::writeMakeParts(QTextStream &t)
             if(!project->isEmpty("QMAKE_POST_LINK"))
                 t << "\n\t" << var("QMAKE_POST_LINK");
             t << endl << endl;
-        } else if(project->isEmpty("QMAKE_HPUX_SHLIB")) {
+        } else if(project->isEmpty("QMAKE_HPUX_SHLIB")
+                      && project->isEmpty("QMAKE_OPENBSD_SHLIB")) {
             t << "\n\t";
 
             if (!project->isActiveConfig("unversioned_libname"))
@@ -639,7 +652,22 @@ UnixMakefileGenerator::writeMakeParts(QTextStream &t)
             if(!project->isEmpty("QMAKE_POST_LINK"))
                 t << "\n\t" << var("QMAKE_POST_LINK");
             t << endl << endl;
+        } else if (!project->isEmpty("QMAKE_OPENBSD_SHLIB")) {
+            // OpenBSD-specific shared libraries handling:
+            // libfoo.so.X.Y, without any symlinks.
+            // 
+            t << "\n\t"
+              << "-$(DEL_FILE) $(TARGET)\n\t"
+              << var("QMAKE_LINK_SHLIB_CMD");
+            if(!destdir.isEmpty())
+                t  << "\n\t-$(DEL_FILE) " << destdir << "$(TARGET)"
+                   << "\n\t-$(MOVE) $(TARGET)  " << destdir;
+            if(!project->isEmpty("QMAKE_POST_LINK"))
+                t << "\n\t" << var("QMAKE_POST_LINK");
+            t << endl << endl;
         } else {
+            // HP-UX-specific shared libraries handling:
+            // libfoo.sl.X, with symlink to libfoo.sl.
             t << "\n\t"
               << "-$(DEL_FILE) $(TARGET) $(TARGET0)\n\t"
               << var("QMAKE_LINK_SHLIB_CMD") << "\n\t";
@@ -1191,6 +1219,11 @@ void UnixMakefileGenerator::init2()
                 project->values("TARGET_x").append("lib" + project->first("TARGET") + "." +
                                                         project->first("VER_MAJ"));
             project->values("TARGET") = project->values("TARGET_x");
+        } else if (!project->isEmpty("QMAKE_OPENBSD_SHLIB")) {
+            project->values("TARGET_x.y").append("lib" + project->first("TARGET") +
+                                                      "." + project->first("QMAKE_EXTENSION_SHLIB") + 
+                                                      ".$(LIB" + project->first("TARGET") + "_VERSION)");
+            project->values("TARGET") = project->values("TARGET_x.y");
         } else if (!project->isEmpty("QMAKE_AIX_SHLIB")) {
             project->values("TARGET_").append(project->first("QMAKE_PREFIX_STATICLIB") + project->first("TARGET")
                     + "." + project->first("QMAKE_EXTENSION_STATICLIB"));
@@ -1381,18 +1414,32 @@ UnixMakefileGenerator::writeLibtoolFile()
       << QT_VERSION_STR << ")";
     t << "\n";
 
+    if (!project->isEmpty("QMAKE_OPENBSD_SHLIB"))
+      t << "LIB" << fileVar("QMAKE_ORIG_TARGET") << "_VERSION="
+        << project->first("VER_MAJ") << "." << project->first("VER_MIN");
+
     t << "# The name that we can dlopen(3).\n"
-      << "dlname='" << fileVar(project->isActiveConfig("plugin") ? "TARGET" : "TARGET_x")
-      << "'\n\n";
+      << "dlname='";
+    if (project->isActiveConfig("plugin"))
+        t << fileVar("TARGET");
+    else if (!project->isEmpty("QMAKE_OPENBSD_SHLIB"))
+        t << fileVar("TARGET_x.y");
+    else
+        t << fileVar("TARGET_x");
+    t << "'\n\n";
 
     t << "# Names of this library.\n";
     t << "library_names='";
     if(project->isActiveConfig("plugin")) {
         t << fileVar("TARGET");
     } else {
-        if (project->isEmpty("QMAKE_HPUX_SHLIB"))
-            t << fileVar("TARGET_x.y.z") << ' ';
-        t << fileVar("TARGET_x") << ' ' << fileVar("TARGET_");
+        if (!project->isEmpty("QMAKE_OPENBSD_SHLIB")) {
+            t << fileVar("TARGET_x.y") << " ";
+        } else {
+            if (!project->isEmpty("QMAKE_HPUX_SHLIB"))
+                t << fileVar("TARGET_x.y.z") << " ";
+            t << fileVar("TARGET_x") << " " << fileVar("TARGET_");
+        }
     }
     t << "'\n\n";
 
